import 'package:flutter/material.dart';

class AnimatedBreath extends StatefulWidget {
  const AnimatedBreath({super.key});

  @override
  _AnimatedBreathState createState() => _AnimatedBreathState();
}

class _AnimatedBreathState extends State<AnimatedBreath>
    with TickerProviderStateMixin {
  late final AnimationController _animationController =
      AnimationController(vsync: this, duration: const Duration(seconds: 4));

  late final AnimationController _optAnimationController =
      AnimationController(vsync: this, duration: const Duration(seconds: 1));
  late final _opacityTween =
      Tween(begin: 1.0, end: 0.5).animate(_optAnimationController);

  void starAnimated() async {
    _animationController.forward();
    await Future.delayed(const Duration(seconds: 4));
    _optAnimationController.repeat(reverse: true);
    await Future.delayed(const Duration(seconds: 4));
    _optAnimationController.stop();
    _animationController.reverse();
  }

  @override
  void initState() {
    // TODO: implement initState
    starAnimated();
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _animationController.dispose();
    _optAnimationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('呼吸动画'),
      ),
      body: Center(
        child: Column(
          children: [
            FadeTransition(
              opacity: _opacityTween,
              child: AnimatedBuilder(
                animation: _animationController,
                builder: (BuildContext context, Widget? child) {
                  return Opacity(
                    opacity: _opacityTween.value,
                    child: Container(
                      width: 200,
                      height: 200,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(100),
                        gradient: RadialGradient(
                          colors: [
                            Colors.blue[600]!,
                            Colors.blue[100]!,
                          ],
                          stops: [
                            _animationController.value,
                            _animationController.value + 0.1
                          ],
                        ),
                      ),
                    ),
                  );
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}
